package com.cloud.code.真题;

import java.util.*;

public class Demo17 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()) {
            int n = Integer.parseInt(scanner.nextLine());
            int k = Integer.parseInt(scanner.nextLine());

            Deque<Integer> deque = new ArrayDeque<>();
            List<Integer> list = new ArrayList<>();
            boolean[] used = new boolean[n];
            int[] nums = new int[n];
            for (int i = 0; i < n; i++) {
                nums[i] = i + 1;
            }

            dfs(nums, n, 0, used, list, deque);
            Collections.sort(list);
            System.out.println(list.get(k-1));

        }
    }

    private static void dfs(int[] nums, int length, int depth, boolean[] used, List<Integer> list, Deque<Integer> deque) {

        if (length == depth) {
            String s = Arrays.toString(new Deque[]{deque}).replaceAll("\\W+", "");
            list.add(Integer.parseInt(s));
            return;
        }

        for (int i = 0; i < length; i++) {

            if (used[i]) {
                continue;
            }

            deque.addLast(nums[i]);
            used[i] = true;
            dfs(nums, length, depth + 1, used, list, deque);
            used[i] = false;
            deque.removeLast();
        }


    }
}
